// settings.js
Page({
  data: {
    settings: {
      dailyGoal: 10,
      autoPlay: false,
      showPhonetic: true,
      showTranslation: false,
      reviewInterval: 1,
      theme: 'default'
    },
    wordList: [],
    showAddWord: false,
    newWord: {
      word: '',
      translation: '',
      phonetic: '',
      difficulty: 1
    }
  },

  onLoad() {
    this.loadSettings()
    this.loadWordList()
  },

  // 加载设置
  loadSettings() {
    const settings = wx.getStorageSync('settings')
    if (settings) {
      this.setData({
        settings: { ...this.data.settings, ...settings }
      })
    }
  },

  // 加载单词列表
  loadWordList() {
    const wordList = wx.getStorageSync('wordData') || []
    this.setData({ wordList })
  },

  // 保存设置
  saveSettings() {
    wx.setStorageSync('settings', this.data.settings)
    wx.showToast({
      title: '设置已保存',
      icon: 'success'
    })
  },

  // 更新设置
  updateSetting(e) {
    const { key } = e.currentTarget.dataset
    const { value } = e.detail
    
    this.setData({
      [`settings.${key}`]: value
    })
    
    this.saveSettings()
  },

  // 切换开关设置
  toggleSetting(e) {
    const { key } = e.currentTarget.dataset
    const currentValue = this.data.settings[key]
    
    this.setData({
      [`settings.${key}`]: !currentValue
    })
    
    this.saveSettings()
  },

  // 显示添加单词界面
  showAddWord() {
    this.setData({
      showAddWord: true
    })
  },

  // 隐藏添加单词界面
  hideAddWord() {
    this.setData({
      showAddWord: false,
      newWord: {
        word: '',
        translation: '',
        phonetic: '',
        difficulty: 1
      }
    })
  },

  // 输入新单词信息
  inputWord(e) {
    const { field } = e.currentTarget.dataset
    const { value } = e.detail
    
    this.setData({
      [`newWord.${field}`]: value
    })
  },

  // 选择难度
  selectDifficulty(e) {
    const { difficulty } = e.currentTarget.dataset
    this.setData({
      'newWord.difficulty': parseInt(difficulty)
    })
  },

  // 添加新单词
  addWord() {
    const { newWord } = this.data
    
    if (!newWord.word || !newWord.translation) {
      wx.showToast({
        title: '请填写单词和翻译',
        icon: 'none'
      })
      return
    }

    const wordData = wx.getStorageSync('wordData') || []
    const newWordData = {
      ...newWord,
      learned: false,
      reviewCount: 0
    }
    
    wordData.push(newWordData)
    wx.setStorageSync('wordData', wordData)
    
    this.loadWordList()
    this.hideAddWord()
    
    wx.showToast({
      title: '添加成功',
      icon: 'success'
    })
  },

  // 删除单词
  deleteWord(e) {
    const { index } = e.currentTarget.dataset
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个单词吗？',
      success: (res) => {
        if (res.confirm) {
          const wordData = wx.getStorageSync('wordData') || []
          wordData.splice(index, 1)
          wx.setStorageSync('wordData', wordData)
          
          this.loadWordList()
          
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          })
        }
      }
    })
  },

  // 编辑单词
  editWord(e) {
    const { index } = e.currentTarget.dataset
    const word = this.data.wordList[index]
    
    this.setData({
      newWord: { ...word },
      showAddWord: true,
      editingIndex: index
    })
  },

  // 保存编辑的单词
  saveEditedWord() {
    const { newWord, editingIndex, wordList } = this.data
    
    if (!newWord.word || !newWord.translation) {
      wx.showToast({
        title: '请填写单词和翻译',
        icon: 'none'
      })
      return
    }

    const wordData = [...wordList]
    wordData[editingIndex] = { ...newWord }
    wx.setStorageSync('wordData', wordData)
    
    this.loadWordList()
    this.hideAddWord()
    
    wx.showToast({
      title: '保存成功',
      icon: 'success'
    })
  },

  // 返回主页
  goBack() {
    wx.navigateBack()
  },

  // 清空所有数据
  clearAllData() {
    wx.showModal({
      title: '确认清空',
      content: '确定要清空所有数据吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync()
          this.loadWordList()
          
          wx.showToast({
            title: '数据已清空',
            icon: 'success'
          })
        }
      }
    })
  }
})
